import { defineStore } from 'pinia'
import {AUTHORIZATION, USER_ID, NICK_NAME} from '@/common_js/common_variable'

export const useCounterStore = defineStore('counter', {
  state: () => {
    return {
      token: null,
      userId: null,
      nickName: null,
      isLogin: false,
    }
  },
  // 也可以这样定义
  // state: () => ({ count: 0 })
  actions: {
    saveLoginInfo(token, userId, nickName) {
      this.token = token
      this.userId = userId
      this.nickName = nickName
      this.isLogin = true
      // sessionStorage.setItem(AUTHORIZATION, token);
      // sessionStorage.setItem(USER_ID, userId);
      // sessionStorage.setItem(NICK_NAME, nickName);
      // 浏览器共享数据
      localStorage.setItem(AUTHORIZATION, token);
      localStorage.setItem(USER_ID, userId);
      localStorage.setItem(NICK_NAME, nickName);
    },
    logout() {
      // sessionStorage.removeItem(AUTHORIZATION);
      // sessionStorage.removeItem(USER_ID);
      // sessionStorage.removeItem(NICK_NAME);
      // 浏览器共享数据
      localStorage.removeItem(AUTHORIZATION);
      localStorage.removeItem(USER_ID);
      localStorage.removeItem(NICK_NAME);
    }
  },
  getters: {
    getToken: (state) => { return state.token },
    getIsLogin: (state) => { return state.isLogin },
    getUserId: (state) => { return state.userId },
    getNickName: (state) => { return state.nickName },
  },
})

